package com.workman.factory;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workman.common.util.HttpUtil;
import com.workman.common.util.StringUtil;
import com.workman.common.util.WorkManConstans;
import org.springframework.data.domain.Sort;

import javax.servlet.http.HttpServletRequest;

/**
 * BootStrap Table默认的分页参数创建
 *
 * @author fengshuonan
 * @date 2017-04-05 22:25
 */
public class PageFactory<T> {

    public Page<T> defaultPage() {
        HttpServletRequest request = HttpUtil.getRequest();
        //每页多少条数据
        long limit = 10L;
        if(request.getParameter("limit")!=null) {
            limit = Long.valueOf(request.getParameter("limit"));
        }
        long pageNum = 0L;

        if(request.getParameter("page")!=null){
            pageNum = Long.valueOf(request.getParameter("page"));
        }

        //排序字段名称
        String sortName = request.getParameter("sort");
        //asc或desc(升序或降序)
        String order = request.getParameter("order");
        if (StringUtil.isNullOrEmpty(sortName) || StringUtil.isNullOrEmpty(order)) {
            Page<T> page = new Page(pageNum, limit);
            return page;
        }

        Page<T> page = new Page<>(pageNum, limit);
        if (WorkManConstans.ORDER_BY_DESC.equals(order)) {
            page.addOrder(OrderItem.desc(sortName));
        } else {
            page.addOrder(OrderItem.asc(sortName));
        }
        return page;

    }
}
